/*
 * @Author: wangc
 * @Date: 2020-07-27 14:41:48
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-08-30 15:47:16
 * @Description: 
 */
package com.example.controller;

import java.util.*;

import com.example.model.LoginResponse;
import com.example.model.MessageEntity;
import com.example.service.MessageService;
import com.example.util.IpUtil;
import com.example.util.RedisUtil;
import com.example.util.RespCode;
import com.example.util.RespEntity;
import com.example.model.User;
import com.example.service.UserService;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class Controller {
    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;

    @Resource
    private RedisUtil redisUtil;

    @RequestMapping("/login")
    @ResponseBody
    public RespEntity login(@RequestBody User user){
        User user2 = userService.checkLogin(user.getUsername(),user.getPassword());
        if(Objects.isNull(user2)){
            return new RespEntity(RespCode.LOGINERROR,"账号密码错误");
        }
        // 使用UUID作为token值
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //存入redis ，有效时间为1个小时
        redisUtil.set(token,user.getUsername(),3600);
        LoginResponse lp = new LoginResponse();
        lp.setUsername(user.getUsername());
        lp.setToken(token);
        RespEntity resp =new RespEntity(RespCode.SUCCESS,lp);
        return resp;
    }

    @RequestMapping("/getAllUser")
    @ResponseBody
    private RespEntity getAllUser() {
        List<User> users =  userService.getAllUser();
        RespEntity resp =new RespEntity(RespCode.SUCCESS,users);
        return resp;

    }
    @RequestMapping("/register")
    @ResponseBody
    private RespEntity register(@RequestBody User user, HttpServletRequest request) {
        User u= userService.getUserbyUsername(user.getUsername());
        if(!Objects.isNull(u)){
            RespEntity resp =new RespEntity(RespCode.NAMEEXIST);
            return resp;
        }
        int result = userService.register(user);
        RespEntity resp =new RespEntity(RespCode.SUCCESS,result);
        return resp;
    }

    @RequestMapping("/getAllMessage")
    @ResponseBody
    private RespEntity getAllMessage() {
        List<MessageEntity> list = messageService.getAllMessage();
        RespEntity resp =new RespEntity(RespCode.SUCCESS,list);
        return resp;
    }

    @RequestMapping("/insertMessage")
    @ResponseBody
    private RespEntity insertMessage(@RequestBody MessageEntity msg, HttpServletRequest request) {
        String ip = IpUtil.getIpAddr(request);
        msg.setIp(ip);
        if(msg.getName() == ""|| msg.getEmail()==""||msg.getMessage()==""){
            RespEntity resp =new RespEntity(RespCode.WARN,ip);
            return resp;
        }
        int result = messageService.insertMessage(msg);
        RespEntity resp =new RespEntity(RespCode.SUCCESS,result);
        return resp;
    }

    @RequestMapping("/deleteMessage")
    @ResponseBody
    private RespEntity deleteMessage(@RequestParam int id) {
        int result = messageService.deleteMessageById(id);
        RespEntity resp =new RespEntity(RespCode.SUCCESS,result);
        return resp;
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    private RespEntity deleteUser(@RequestParam int id) {
        int result = userService.deleteUserById(id);
        RespEntity resp =new RespEntity(RespCode.SUCCESS,result);
        return resp;
    }
}
